/**
 *
 */
package de.cbf.cam.dsa.rcp.model.entities;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;

import de.cbf.util.uuidheader.entities.UUIDHeader;
import de.cbf.util.uuidheader.exceptions.EntityMergeException;

/**
 * @author salter
 * Format positions must match format string so for every %s there must<br>
 *  be a corresponding position in the name map to choose the name from
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class NameFormat extends UUIDHeader {

	public static final String ATTRIBUTE_ROYAL 	= "royal";
	public static final String ATTRIBUTE_FEMALE = "female";
	public static final String ATTRIBUTE_MALE	= "male";
	@XmlElementWrapper(name="nameTypes")
	@XmlElement(name="nameType")
	protected List<NameType> formatNameTypes;
	@XmlElementWrapper(name="attributes")
	@XmlElement(name="attribute")
	protected List<String> 	attributes;


	@Override
	protected void doMergeDataFrom(UUIDHeader entity, boolean i_am_older)
			throws EntityMergeException {
		if (entity instanceof NameFormat) {
			NameFormat other = (NameFormat) entity;
			if(i_am_older){
				this.formatNameTypes = other.formatNameTypes;
				attributes.clear();
				attributes.addAll(other.attributes);
			}
		}else{
			throw new EntityMergeException("Entity does not fit class");
		}
	}
	@Override
	public Object clone() throws CloneNotSupportedException {
		NameFormat clone = new  NameFormat();
		try {
			clone.mergeDataFrom(this);
		} catch (EntityMergeException e) {
			throw new CloneNotSupportedException(e.getLocalizedMessage());
		}
		return clone;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((attributes == null) ? 0 : attributes.hashCode());
		result = prime * result
				+ ((formatNameTypes == null) ? 0 : formatNameTypes.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		NameFormat other = (NameFormat) obj;
		if (attributes == null) {
			if (other.attributes != null)
				return false;
		} else if (!attributes.equals(other.attributes))
			return false;
		if (formatNameTypes == null) {
			if (other.formatNameTypes != null)
				return false;
		} else if (!formatNameTypes.equals(other.formatNameTypes))
			return false;
		return true;
	}


	@Override
	public String toString() {
		return "NameFormat [attributes=" + attributes + "]";
	}

	public List<NameType> getFormatNameTypes() {
		return formatNameTypes;
	}
	public void setFormatNameTypes(List<NameType> nameTypes) {
		this.formatNameTypes = nameTypes;
	}

	public List<String> getAttributes() {
		return attributes;
	}
	public void setAttributes(List<String> attributes) {
		this.attributes = attributes;
	}







}
